import java.io.*;
import java.net.*;

class ServerConnection extends Connection {
    private ServerSocket ListeningSocket = null;
    private int serverPort = 0;
    private int serverTimeout = 0;
    
    public ServerConnection(int Port, int Timeout) {
        this.serverTimeout = Timeout;

        try {
            ListeningSocket = new ServerSocket(Port);
            ListeningSocket.setSoTimeout(this.serverTimeout);

            // If port == 0 is parsed then the ServerSocket will find a free port
            // After creation need to find what that port is
            this.serverPort = ListeningSocket.getLocalPort();
        } catch (IOException ioe) {
            System.err.println("Error creating ServerConnection Object, details follow:");
            ioe.printStackTrace();
        }
    }
    
    public boolean waitForConnection() {
        System.out.println("Waiting for client connection on port " + myPort());
        
        // init connection
        try {
            if(initConnection(ListeningSocket.accept())) {
                System.out.println("Connection established.");
            } else {
                System.err.println("Connection failed.");
                return false;
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
        
        return true;
    }
    
    public static void main(String args[]) {
        ServerConnection sc = new ServerConnection(8189, 0);
        sc.run();
    }
    
    public void run() {
        Socket tmp = null;
        
        if(!waitForConnection()) {
            System.err.println("Error waiting for connection.");
            return;
        }
        
        Pack[] p = null;
        int i;
        while(isConnected()) {
            // any data to forward to client?
            p = recieveData();
            if(p != null) {
                for(i = 0; i < p.length; i++) {
                    if(p[i].requestID == Pack.CLOSE_CONNECTION_ID) {
                        closeConnection();
                        closeServerConnection();
                        break;
                    } else if(p[i].requestID == Pack.FORWARD_DATA_ID) {
                        //CentralHub.addPack(p);
                        //log.log("Added pack to central hub, details of pack follow:");
                        //log.log(p.toString());
                        System.out.println("::Recieved Data::\n" + p[i]);
                    } else {
                        System.out.println("::Recieved Data::\n" + p[i]);
                    }
                }
            }
        }
    }
    
    public boolean closeServerConnection() {
    //  Close the Socket & ServerSocket objects
    //  Returns: true if successful, false if not
        try {
            ListeningSocket.close();
        } catch (IOException ioe) {
            //log.log(ioe);
            ioe.printStackTrace();
            return false;
        }

        ListeningSocket = null;
        System.out.println("ServerConnection shutdown was successful.");
        return true;
    }
}
